package com.dao;

import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.DBUtils.DBUtil;
import com.mode.Students;

public class DBOperation<T> {
	private Class<T> entity;
	private File[] fd;
	private String tableName;
	
	
	public  DBOperation(){
		ParameterizedType type=(ParameterizedType) this.getClass().getGenericSuperclass();
		entity=(Class<T>) type.getActualTypeArguments()[0];
		tableName=entity.getSimpleName();
	}
	
	
	public List<Object> qry(T o){
		List<Object> list=new ArrayList<>();
		StringBuffer sql=new StringBuffer("select*from"+tableName);
		try {
			PreparedStatement pstm=DBUtil.getconn().prepareStatement(sql.toString());
			ResultSet re=pstm.executeQuery();
			while(re.next()){
				Students stu=new Students();
				stu.setId(re.getInt("id"));
				stu.setName(re.getString("name"));
				stu.setAge(re.getInt("age"));
				stu.setSex(re.getString("sex"));
				list.add(stu);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
